﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace ExcelExpoter.Editor
{
    [Serializable]
    public class ExcelInfo
    {
        public string Name;
        public int Hash;
    }
    public class ExporterHash : ScriptableObject
    {
        public List<ExcelInfo> ExporterFiles = new List<ExcelInfo>();
        public Dictionary<string, string> SheetHashDic = new Dictionary<string, string>();

        [HideInInspector]
        public bool IsGenerateByte = false;
        [HideInInspector]
        public bool IsGenerateText = false;

        public static ExporterHash CreateNew(ExporterType type)
        {
            var obj = ScriptableObject.CreateInstance<ExporterHash>();
            
            var path = $"{ExporterTool.AssetPath}/Resources/Configs";
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            
            var config = type == ExporterType.Lua ? ExporterPath.ExporterLuaHashConfig : ExporterPath.ExporterCsHashConfig;
            AssetDatabase.CreateAsset(obj, $"{path}/{config}.asset");
            return obj;
        }

        public bool IsHashSame(string n, int hash)
        {
            if (this.TryGetExcelInfo(n, out var hashCode))
            {
                if (hashCode.Hash.Equals(hash))
                {
                    return true;
                }
                return false;
            }
            return true;
        }

        public bool IsNewExcel(string n)
        {
            foreach (var excel in this.ExporterFiles)
            {
                if (excel.Name.Equals(n))
                {
                    return false;
                }
            }
            return true;
        }

        public bool IsSheetHashSame(string sheetName, string hash)
        {
            if (this.SheetHashDic.TryGetValue(sheetName, out var hashCode))
            {
                if (hash.Equals(hashCode)) return true;
                this.SheetHashDic[sheetName] = hash;
                return false;
            }
            this.SheetHashDic.Add(sheetName, hash);
            return false;
        }

        public bool TryGetExcelInfo(string n, out ExcelInfo excelInfo)
        {
            foreach (var excel in this.ExporterFiles)
            {
                if (excel.Name.Equals(n))
                {
                    excelInfo = excel;
                    return true;
                }
            }
            excelInfo = null;
            return false;
        }

        public void Add(string n, int hash)
        {
            this.ExporterFiles.Add(new ExcelInfo(){Name = n, Hash = hash});
        }

        public void Remove(string n)
        {
            for (int i = 0; i < this.ExporterFiles.Count; i++)
            {
                if (this.ExporterFiles[i].Name == n)
                {
                    this.ExporterFiles.RemoveAt(i);
                    return;
                }
            }
        }
        
        public void Save()
        {
            EditorUtility.SetDirty(this);
            AssetDatabase.SaveAssets();
        }
    }
}